package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.Customer;
import org.example.erp.domain.Supplier;
import org.example.erp.service.impl.SupplierServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class SupplierConfig {
    @Autowired
    private SupplierServiceImpl supplierService;

    @GetMapping("/vendorManagement/find")
    public DataResult find(@RequestParam(value = "currentPage", defaultValue = "1")Integer currentPage,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(required = false) String supplierCode,
                           @RequestParam(required = false) String supplierName,
                           @RequestParam(required = false) String supplierAbbreviation,
                           @RequestParam(required = false) Integer status
    ){
        LambdaQueryWrapper<Supplier> customerWrapper = new LambdaQueryWrapper<>();
        IPage<Supplier> page = new Page<>(currentPage, pageSize);
        customerWrapper.eq(!"".equals(supplierCode) && supplierCode!=null,Supplier::getSupplierCode,supplierCode)
                .eq(!"".equals(supplierName) && supplierName!=null,Supplier::getSupplierName,supplierName)
                .eq(!"".equals(supplierAbbreviation) && supplierAbbreviation!=null,Supplier::getSupplierAbbreviation,supplierAbbreviation)
                .eq(!"".equals(status) && status!=null,Supplier::getStatus,status);
        IPage<Supplier> pageResult = supplierService.page(page, customerWrapper);
        return DataResult.success(pageResult);
    }




    @PostMapping("/vendorManagement/insert")
    public DataResult insert(@RequestBody Supplier Supplier){
        System.out.println("添加客户");
        supplierService.save(Supplier);
        return DataResult.success();
    }

    @PutMapping("/vendorManagement/update")
    public DataResult update(@RequestBody Supplier Supplier){
        supplierService.updateById(Supplier);
        return DataResult.success();
    }


    @DeleteMapping("/vendorManagement/delete")
    public DataResult delete(@RequestBody List<Integer> logIds) {
        supplierService.removeByIds(logIds);
        return DataResult.success();
    }
}
